package battleship;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Grid 
{
	private int n = ConfigureGame.HIGHER_BOUNDARY - ConfigureGame.LOWER_BOUNDARY +1;
	private BlockInterface[][] table;
	private int ships_on_grid;
	private static boolean flag = true;
	
	public static void gameOver()
	{
		flag = false;
	}
	
	public Grid()
	{
		this.table = new Block[n][n];
		this.ships_on_grid = 0;
	}
	
	public void positionShip(Ship ship) throws IncorrectPositionException
	{
		
		this.ships_on_grid++;
		ship.positionShip(this.table);
		//System.out.println("Nave in joc: "+this.ships_on_grid);
	}
	
	public void fire()
	{
		int cx,cy,code;
		int repeat = 0;
		BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
		try
		{
			System.out.println("Se introduc coordonatele blocului atacat!");
			do
			{
				repeat = 0;
				System.out.println("Coordonata Verticala (cx):= ");
				cx = Integer.parseInt(buff.readLine());
				System.out.println("Coordonata Orizontala (cy):= ");
				cy = Integer.parseInt(buff.readLine());
				
				if (cx <= ConfigureGame.HIGHER_BOUNDARY && cx >= ConfigureGame.LOWER_BOUNDARY &&
					cy <= ConfigureGame.HIGHER_BOUNDARY && cy >= ConfigureGame.LOWER_BOUNDARY)
				{
					System.out.println("Blocul ("+cx+","+cy+") a fost atacat!\nRezultat: ");
					if(table[cx-1][cy-1] == null)
					{
						System.out.println("MISSED!");
						table[cx-1][cy-1] = new Block(ConfigureGame.DEAD_BLOCK);
					}
					else
					{
						code = table[cx-1][cy-1].destroyBlock();
						if(code != ConfigureGame.DEAD_BLOCK)
						{
							table[cx-1][cy-1] = new Block(ConfigureGame.DEAD_BLOCK);
							if(code == ConfigureGame.SHIP_SUNK_CODE)
							{
								this.ships_on_grid--;
								if (this.ships_on_grid == 0)
									Grid.gameOver();
							}
						}
						else
						{
							System.out.println("Aici s-a mai tras!\nIntroduceti noile coordonate: ");
							repeat = 1;
						}
					}
				}
				else
				{
					System.out.println("Coordonate incorecte!");
					repeat = 1;
				}
			}while(repeat == 1);
		}catch(IOException e){
			
		}
	}
	
	public static void main(String[] args) 
	{
		String s;
		int i,id;
		try
		{
			BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream("conf_battleship.txt")));
			ConfigureGame.LOWER_BOUNDARY = Integer.parseInt(file.readLine());
			ConfigureGame.HIGHER_BOUNDARY = Integer.parseInt(file.readLine());
			ConfigureGame.NR_SHIPS = Integer.parseInt(file.readLine());
			ConfigureGame.SHIP_NR_BLOCKS = new int [ConfigureGame.NR_SHIPS];
			for(i=0;i<ConfigureGame.NR_SHIPS;i++)
			{
				s=file.readLine();
				StringTokenizer st = new StringTokenizer(s);
				id=Integer.parseInt(st.nextToken());
				ConfigureGame.SHIP_NR_BLOCKS[id] = Integer.parseInt(st.nextToken());
				
			}
			
		}catch(IOException e){
			
		}
		try
		{
			Grid grid = new Grid();
			Ship mines = new Minesweeper();
			grid.positionShip(mines);
			Ship battle = new Battleship();
			grid.positionShip(battle);
			Ship destr = new Destroyer();
			grid.positionShip(destr);
			
			while(flag)
				grid.fire();
			System.out.println(" SURRENDER !!!\n GAME OVER !!! ");
		}catch(IncorrectPositionException e)
		{
			System.out.println(e);
		}
   }
}		
